/************************************************/
/************************************************/
/**** PART 2: GENERATE VARIABLES  ****/
/************************************************/
/************************************************/



/*************************************************/
/*** race status ****/
/*************************************************/
	tab patracegroup
	tab phyracegroup

	gen SameRace=(patracegroup==phyracegroup)
	replace SameRace=. if patracegroup==.|phyracegroup==. //missing race 
	replace SameRace=. if phyracegroup==3&patracegroup==3 //both belong to "others"
	
	label var patracegroup "patient race group"
	label var phyracegroup "physician race group"
	label var SameRace "Patient-physicain race concordance"


/*************************************************/
/*** time catgories: week&month&year ****/
/*************************************************/

	gen consultstartdow=dow(consultstartdate)
	label var consultstartdow "consultation start day of week"
	label define dow  0 "Sunday"  1 "Monday" 2 "Tuesday"  3 "Wednesday" 4 "Thursday" 5 "Friday" 6"Saturday" 
	label values consultstartdow dow

	gen consultstartmonth=mofd(consultstartdate)
	format consultstartmonth %tm
	label var consultstartmonth "consultation start month by year"

	gen consultstarthour=hh(consultstartdate)
	label var consultstarthour "consultation start hour of day"

	gen consultstartyear=year(consultstartdate)



/*************************************************/
/*** physician decisions ****/
/*************************************************/
*admission
	tab dispositiontype
	gen Admit=(dispositiontype=="Admitted")
	label var Admit "Inpatient admission (dummy)"

*consultation length
	gen double Consulttime=(consultenddatetime-consultstartdatetime)/60000 
	sum Consulttime,detail 
	gen lnConsulttime=ln(Consulttime+1)
	label var Consulttime "Length of consultation (minutes)"
	label var lnConsulttime "Log (length of consultation+1)"

*diagnostic testing
	gen ADI=(CTCount+MRCount>0) 
	label var ADI "Advance diagnostic imaging (dummy)"

	gen LabUse=(LabCount>0) 
	label var LabUse "Lab test utilization (dummy)"

	gen ImageUse=(CTCount+MRCount+USCount+XrayCount>0) 
	label var ImageUse "Imaging utilization (dummy)"

	gen TestCount=CTCount+MRCount+USCount+XrayCount+LabCount
	label var TestCount "Number of diagnostic tests"
	
	
	
/*************************************************/
/*** patient characteristics ****/
/*************************************************/

***gender
	tab gender
	gen Male=(gender=="Male"|gender=="M")
	label var Male "Patient gender(dummy)"

***age 
	gen birthdate=date(dob,"YMD") if substr(dob,5,1)=="-"
	replace birthdate=date(dob,"DMY") if substr(dob,5,1)!="-"
	format birthdate %td
	gen birthyear=year(birthdate)
	
	gen age=consultstartyear-birthyear
	sum age,detail

	gen agegroup=(age<=14) //children
	replace agegroup=2 if age>14&age<=29
	replace agegroup=3 if age>29&age<=44
	replace agegroup=4 if age>44&age<=59
	replace agegroup=5 if age>59

	label define agelabel 1 "less than 15" 2 "15 to 29" 3 "30 to 44" 4 "45 to 59" 5 "60 and above"
	label list agelabel
	label values agegroup agelabel
	
	label var age "Patient age (years)"
	label var agegroup "Patient age groups"

***triage severity
	tab severity
	gen severitylevel=1 if severity=="P1"
	replace severitylevel=2 if severity=="P2"
	replace severitylevel=3 if severity=="P3"
	label var severitylevel "triage severity level"

	gen severecase=(severitylevel<3)
	label var severecase "severe case (dummy)"

***patient diagnoses

*detailed diagnosis
	gen digit=strlen(diagcode) //number of digits 
	gen detailedICD=(digit>=4)
	replace detailedICD=0 if digit==4&substr(diagcode,1,1)=="E"
	label var detailedICD "detailed diagnosis (dummy)"


*broad diagnostic category

	ren diagnosis_broad BrDx
	label var BrDx "Broad diagnostic category"
	destring BrDx,replace
	labmask BrDx, values(diagnosis_broadlabel)

	
*specific diagnostic category

	gen SpDx=1 if strpos(diagnosislabel,"Intestinal infection")
	replace SpDx=2 if diagnosis_broadlabel=="Diseases of the respiratory system" 
	replace SpDx=3 if strpos(diagnosislabel,"Superficial injury")
	replace SpDx=4 if strpos(diagnosislabel,"External causes of injury")

	label define diagnosis 1 "Intestinal infection" 2 "Diseases of the respiratory system" ///
	3 "Superficial injury" 4 "External causes of injury"
	label list diagnosis
	label values SpDx diagnosis
	label var SpDx "Specific diagnosis"




/*************************************************/
/*** patient outcomes ****/
/*************************************************/

***death
	tab dispositiontype
	gen Death=(strpos(dispositiontype,"Death"))
	label var Death "Death in the ED (dummy)"
	
***ED revisit

*patient identity
	bysort address Male birthdate patracegroup: gen patid=(_n==1)
	replace patid=sum(patid)

*7-day revisit after discharge
	sort patid consultenddatetime
	bysort patid: gen Revisit=((visitdatetime[_n+1]-consultenddatetime)/60000<=7*24*60) if dispositiontype=="Discharged"
	label var Revisit "7-day revisit after ED discharge (dummy)"


	
/*************************************************/
/*** construct instrument variable ****/
/*************************************************/

***categorize physician-shifts by treatment area: severe vs. nonsevere

	bysort phyid shiftnumber: egen shiftseverity=total(severecase)
	bysort phyid shiftnumber: replace shiftseverity=shiftseverity/_N

	sum shiftseverity,d
	sum shiftseverity if shiftseverity<1&shiftseverity>0,d //cross-cover treatment areas
	gen shifttype=(shiftseverity>=0.5)


***count number of on-shift physicians by treatment area
	
	gen phynumb=0 // #physicians in the respective treatment area
	gen phynumb_samerace=0 // #physicians same race as the index patient 
    bysort phyid shiftnumber: gen d=_n==1 
	tab d //#physician-shift: 17059

	gsort -d
	
	forvalues i=1(1)264115{
		  	  
	  count if (consultstartdatetime[`i']>=shiftstart&consultstartdatetime[`i']<=shiftend) ///
	           &(shifttype==severecase[`i']) in 1/17059 
			   
	  replace phynumb = r(N)   in `i' 
	 
	  count if (consultstartdatetime[`i']>=shiftstart&consultstartdatetime[`i']<=shiftend) ///
	           &(shifttype==severecase[`i'])&phyracegroup==patracegroup[`i'] in 1/17059
			   
	  replace phynumb_samerace = r(N)   in `i' 
	  

	}
	
***generate IV

    gen Proportion_Samerace=phynumb_samerac/phynumb
    label var Proportion_Samerace "% same-race physicians"
	
	

/*************************************************/
/*** physician experience, fatigue, and EOS ****/
/*************************************************/

***physician experience

	gen phy_exp=visityear-grad_year
	egen median_exp= median(phy_exp)
	gen experienced=(phy_exp>=median_exp)
	label var experienced "Above-median experience (dummy)"


***workhour 

	gen workhour=floor(hours(consultstartdatetime-shiftstart)/2)
	replace workhour=5 if workhour>5&workhour!=.
	label var workhour "number of hours worked"

	label define hourlabel 0 "less than 2" 1 "2 to 4" 2 "4 to 6" 3 "6 to 8" 4 "8 to 10" 5 "10 and above"
	label list hourlabel
	label values workhour hourlabel

	
***#patients treated

	sort phyid shiftnumber consultstartdatetime consultenddatetime
	bysort phyid shiftnumber: gen priorcase=_n-1
	label var priorcase "number of patients treated"

	
***hours relative to end of shift

	gen irelto=ceil(hours(shiftend-consultstartdatetime))
	replace irelto=3 if irelto>3&irelto!=.
	label var irelto "hours relative to EOS"

	label define eoslabel 1 "last hour" 2 "second hour" 3 "third hour or more"
	label list eoslabel
	label values irelto eoslabel 


